# Replication code for Christopher Chambers-Ju & Mart Trasberg manuscript "Framing Labor Action" (Last updated Feb 15, 2025)
#  Appendix Tables and Balance Table

install.packages("foreign")
install.packages("haven")
install.packages("broom")
library("haven")

# Loading data


# file.choose()

dta <- read_dta("C:/Users/L03538527/OneDrive - Instituto Tecnologico y de Estudios Superiores de Monterrey/Research/Education in Mexico/Drafts/Perspectives on Politics/Analysis/OA23 Base UT-TEC.dta")

# dta<-OA23_Base_UT_TEC
attach(dta)
objects()


# Load required libraries
library(plyr)
library(dplyr)

# Relabeling Treatments

TratUT2<-as.factor(TratUT2)
levels(TratUT2)
library(dplyr)
library(plyr)
dta$Treatment<-revalue(TratUT2, c("1"="Control","2"="Strike","3"="Pro_strike",
                                  "4"="Anti_strike","5"="Double_frame"))

sum(is.na(dta$Treatment))

dta$control<-ifelse(dta$Treatment=="Control", 1,0)
dta$strike<-ifelse(dta$Treatment=="Strike", 1,0)
dta$pro_strike<-ifelse(dta$Treatment=="Pro_strike", 1,0)
dta$anti_strike<-ifelse(dta$Treatment=="Anti_strike", 1,0)
dta$double<-ifelse(dta$Treatment=="Double_frame", 1,0)


# Support for union influence

UT2_1<-as.numeric(UT2_1)
dta$unions <- dta$UT2_1

# Recode the new variable, so higher values are stronger support for unions

dta$unions <- ifelse(dta$unions == 1, 5,
                     ifelse(dta$unions == 2, 4,
                            ifelse(dta$unions == 3, 3,
                                   ifelse(dta$unions == 4, 2,
                                          ifelse(dta$unions == 5, 1, NA)))))

# New variable checks with the old variable

dta$unions<-factor(dta$unions, ordered=T, 
                   levels = c(1:5))
table(dta$unions,dta$UT2_1)
class(dta$unions)

# Recoding support for striking teachers

table(dta$UT2_2) # 1 = totally oppose, 7 = totally support, 8 = NA
dta$Support_teachers<-ifelse(UT2_2 == 8, NA, UT2_2)
table(dta$Support_teachers)
class(dta$Support_teachers)

# Dummy for woman
summary(d1) 
dta$dummy_woman<-ifelse(d1==2,1,0)
summary(dta$dummy_woman)
class(dta$dummy_woman)
dta$woman<-dta$dummy_woman
sum(is.na(dta$woman))

# Dummy for income
dta$income<-as.numeric(dta$d12)
class(dta$income)

dta$income<-ifelse(dta$income==9, NA, dta$income)
sum(is.na(dta$income))

# Age by levels

dta$age_levels <- factor(ifelse(dta$EdadRec == 1, "18 to 29",
                                      ifelse(dta$EdadRec == 3, "over 50", "30 to 49")))

dta$age_levels<-factor(dta$age_levels,
                       ordered = TRUE,
                       levels = c("18 to 29", "30 to 49","over 50"))

table(dta$age_levels)
table(dta$EdadRec)

dta$age<-dta$d2

sum(is.na(dta$d2))

# Dummy for supports Morena
dta$Morena<-factor(ifelse(PartyID==7 |PartyID==8,"Morena","Not Morena"))

dta$Morena<-ifelse(PartyID==7 |PartyID==8,1,0)

# Education by level

table(dta$d3) 
length(dta$d3)
dta$education_levels <- factor(ifelse(dta$d3 == 1, "basica",
                                      ifelse(dta$d3 == 2, "basica",
                                             ifelse(dta$d3 == 3, "basica",
                                                    ifelse(dta$d3 == 4, "basica",
                                                           ifelse(dta$d3 == 5, "basica",
                                                                  ifelse(dta$d3 == 6, "media", 
                                                                         ifelse(dta$d3 == 7, "media", 
                                                                                ifelse(dta$d3 == 8, "media",
                                                                                       ifelse(dta$d3 == 9, "superior",
                                                                                              ifelse(dta$d3 == 10, "superior",
                                                                                                     ifelse(dta$d3 == 11, "superior",
                                                                                                           ifelse(dta$d3 == 12, "superior", NA)))))))))))))
                                                                                                                      
length(dta$education_levels)
dta$education_levels<-factor(dta$education_levels,
                             ordered = TRUE,
                             levels = c("basica", "media","superior"))

sum(is.na(dta$education_levels))

table(dta$education_levels)
dta$education<-as.integer(dta$education_levels)
table(dta$education)



## TABLE A1 ##

install.packages("stargazer")  
library(stargazer)

library(MASS)

m1 <- polr(unions ~ strike + pro_strike + anti_strike + double, data=dta, Hess=TRUE)

summary(m1)


m2 <- dta %>% 
  polr(unions ~ strike + pro_strike + anti_strike + double, data = ., weights = Ponderador, Hess = TRUE)


m3<-polr(unions ~ strike + pro_strike + anti_strike + double + 
  woman + age + income + education + Morena, data=dta, Hess=TRUE)

m4<-polr(unions ~ control + strike + anti_strike + double, data=dta, Hess=TRUE)

m5<-dta %>% 
  polr(unions ~ control+ strike + anti_strike + double, data = ., weights = Ponderador, Hess = TRUE)


m6<-polr(unions ~ control + strike + anti_strike + double + 
  woman + age + income + education + Morena, data=dta, Hess=TRUE)

stargazer(m1,m2,m3,m4,m5,m6, type="html", out="table_1A_new.htm")

browseURL("table_1A_new.htm")


## Table A2 ##

m7 <- polr(factor(Support_teachers) ~ strike + pro_strike + anti_strike + double, data=dta, Hess=TRUE)

m8<- dta %>% 
  polr(factor(Support_teachers) ~ strike + pro_strike + anti_strike + double, data = ., weights = Ponderador, Hess = TRUE)


m9<-polr(factor(Support_teachers) ~ strike + pro_strike + anti_strike + double + 
           woman + age + income + education + Morena, , data=dta, Hess=TRUE)

m10<-polr(factor(Support_teachers) ~ control + strike + anti_strike + double, data=dta, Hess=TRUE)

m11 <-dta %>% 
  polr(factor(Support_teachers) ~ control+ strike + anti_strike + double, data = ., weights = Ponderador, Hess = TRUE)

m12<-polr(factor(Support_teachers) ~ control + strike + anti_strike + double + 
           woman + age + income + education + Morena, data=dta, Hess=TRUE)

stargazer(m7,m8,m9,m10,m11,m12, type="html", out="table_A2_new.htm")

browseURL("table_A2_new.htm")




## Table A3 ##
# Tamaulipas excluded #

m1_tam <- polr(unions ~ strike + pro_strike + anti_strike + double, 
               data = dta %>% filter(Entidad != 28), 
               Hess = TRUE)

m2_tam <- polr(unions ~ control + strike + anti_strike + double, 
               data = dta %>% filter(Entidad != 28), 
               Hess = TRUE)

m3_tam <- polr(factor(Support_teachers)~ strike + pro_strike + anti_strike + double, 
               data = dta %>% filter(Entidad != 28), 
               Hess = TRUE)

m4_tam <- polr(factor(Support_teachers) ~ control + strike + anti_strike + double, 
               data = dta %>% filter(Entidad != 28), 
               Hess = TRUE)

stargazer(m1_tam,m2_tam,m3_tam,m4_tam, type="html", out="table_A3_new.htm")

browseURL("table_A3_new.htm")


## Table A4

# Union Influence

# Mean of unions for each category
mean_unions_Tr_2<-dta   %>% 
  group_by(dta$Treatment) %>%
  dplyr::summarize(Mean = mean(unions, na.rm=TRUE))  

mean_unions_Tr_2
# Mean Control for unions = 3.25

# Enter the data
treatment_data <- data.frame(
  Treatment = c("Control", "Strike", "Pro_strike", "Anti_strike", "Double_frame"),
  Value = c(3.25, 3.08, 3.41, 2.96, 3.17)
)

# Extract the Control value
control_value <- treatment_data %>%
  filter(Treatment == "Control") %>%
  pull(Value)

# Subtract the Control value from all other values
treatment_data <- treatment_data %>%
  mutate(Difference_from_Control = Value - control_value)

# Difference  from the Control conditions
print(treatment_data)


# Striking Teacher Support

# Mean of support for each category
mean_support_Tr_2<-dta   %>% 
  group_by(dta$Treatment) %>%
  dplyr::summarize(Mean = mean(Support_teachers, na.rm=TRUE))  

mean_support_Tr_2
# Mean Control for unions = 3.25


# Enter the data
treatment_data <- data.frame(
  Treatment = c("Control", "Strike", "Pro_strike", "Anti_strike", "Double_frame"),
  Value = c(4.36, 4.18, 4.91, 4.40, 4.43)
)

# Extract the Control value
control_value <- treatment_data %>%
  filter(Treatment == "Control") %>%
  pull(Value)

# Subtract the Control value from all other values
treatment_data <- treatment_data %>%
  mutate(Difference_from_Control = Value - control_value)

# Difference  from the Control conditions
print(treatment_data)

## Table A5 ##
## Balance Tests

install.packages("tableone")
library(tableone)
library(dplyr)

dta$Treatment
dta$woman<-factor(dta$woman)
dta$age_levels
dta$income
dta$education_levels
dta$Morena<-factor(dta$Morena)

balance_table <- CreateTableOne(data = dta,
                                vars = c("woman", "age_levels", "education_levels"),
                                factorVars = c("woman", "education_levels","age_levels"),
                                addOverall = T
)

balance_table

# The data from the census was provided in the technical document by Data OPM
# "Encuesta Nacional DATA Opinión Pública y Mercados NOTA METODOLÓGICA Ómnibus Nacional 2023"

tab_csv <- print(balance_table,
                 printToggle = FALSE,
                 smd = TRUE)

  
## Table A6 ##


balance_table_A5 <- CreateTableOne(data = dta,
                                vars = c("woman", "age_levels", "education_levels"),
                                factorVars = c("woman", "education_levels","age_levels"),
                                strata = "Treatment",
                                addOverall = T
)

balance_table_A5
